//app.js
App({
  onLaunch: function (opt) {
    // 展示本地存储能力
    var that = this;
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    console.log('群场景shareTicket',opt.scene)
    if(opt.scene == 1044){
        console.log('群场景shareTicket',opt.shareTicket)
        that.globalData.shareTicket = opt.shareTicket
    }
      var resetChallengeObj = function () {
          var myTime = new Date();
          var today = myTime.getDate();

          wx.getStorage({
              key:'ChallengeObj',
              success:function (res) {
                  var ChallengeObj =JSON.parse( res.data)
                  console.log('ChallengeObj',ChallengeObj)
                  if (today != ChallengeObj.day){
                      ChallengeObj= {
                          day:today,
                          ChallengeTime :3
                      }
                  }
                  wx.setStorage({
                      key:'ChallengeObj',
                      data:JSON.stringify(ChallengeObj)
                  })
                  that.globalData.ChallengeObj = ChallengeObj

              },fail:function (res) {
                  console.log(res)
                  var ChallengeObj = {
                      day:today,
                      ChallengeTime:3
                  }
                  wx.setStorage({
                      key:'ChallengeObj',
                      data:JSON.stringify(ChallengeObj)
                  })
                  that.globalData.ChallengeObj = ChallengeObj
              }
          })

      }
      resetChallengeObj()
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log("登录成功:",res)
        that.globalData.code = res.code
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              console.log("获取用户信息:",res)
              that.globalData.encryptedData = res.encryptedData
              that.globalData.iv = res.iv
              that.globalData.userInfo = res.userInfo
              that.globalData.userInfo.poss = 0
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (that.userInfoReadyCallback) {
                  that.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    Sharing:null
  }
})